package com.example.server.controller;

import com.example.server.pojo.PageParam;
import com.example.server.pojo.RespBean;
import com.example.server.pojo.TestCase;
import com.example.server.service.TestCaseService;
import com.example.server.util.RunTestngXml;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/api")
public class TestCseCtl {
	@Resource
	private TestCaseService testCaseService;

	@ApiOperation(value = "查询所有用例")
	@GetMapping("/allcase")
	public RespBean allCase(){
		List<TestCase> result = testCaseService.allCase();
		log.info("-----结果："+result);

		if (result != null){
			return new RespBean(200,"success",result);
		}else {
			return new RespBean(400,"fail",null);
		}
	}

	@ApiOperation(value = "分页查询用例列表")
	@GetMapping("/allcaselist")
	public RespBean allCaseList(PageParam pageParam){
		Map result = testCaseService.allCaseList(pageParam);
		log.info("-----结果："+result);

		if (result != null){
			return new RespBean(200,"success",result);
		}else {
			return new RespBean(400,"fail",null);
		}
	}

	@ApiOperation(value = "新增用例")
	@PostMapping("/addcase")
	public RespBean addCase(TestCase testCase){
		int result = testCaseService.addCase(testCase);
		log.info("-----结果："+result);

		if (result > 0 ){
			return new RespBean(200,"success",result);
		}else {
			return new RespBean(400,"fail",null);
		}
	}

	@ApiOperation(value = "调试用例")
	@GetMapping("/runallcase")
	public RespBean runAllCase(TestCase testCase) throws IOException {
		RunTestngXml.runXml();
		log.info("等待调试结果：");
		return new RespBean(200,"success");
	}

	@ApiOperation(value = "调试用例")
	@GetMapping("/debugcase")
	public RespBean debugCase(TestCase testCase){
		log.info("等待调试结果：");
		return new RespBean(200,"success");
	}
}
